// pages/openShop/openInfo.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
import {
  throttle
} from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: null,
    listObj: {},
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    app.login(function (userInfo) {
      console.log(userInfo, 'userInfo')
      that.setData({
        userInfo: userInfo
      });
    });


    // if (options.index) {
    //   this.setData({
    //     index: options.index
    //   })
    this.getData()

    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    let meIsShow = wx.getStorageSync('meIsShow')
    that.setData({
      meIsShow: meIsShow
    });
  },
  getData(index) {
    // /api/ymt/kd
    let t = this
    wx.request({
      url: rootDocment + '/api/llgn/tiaojian',
      data: {},
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res);
        console.log(res.data);
        let arr = res.data.data.tiaojian.split('#')
        // .split(' ↵')
        console.log(arr);
        t.setData({
          listObj: res.data.data,
          arr: arr
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  to: throttle(async function () {
    let that = this
    let isphone = await this.isPhone()
    if (!isphone){
      return
    }
    // console.log(e.currentTarget.dataset.index);
    // let index = this.data.index
    // wx.navigateTo({
    //   url: `/pages/openShop/shopInfo?index=${index}`,
    // })
    let orderObj = await this.createOrder()
    let paraArr = new Array();
    paraArr['order_sn'] = orderObj.sn;
    let sign = app.signature(paraArr);
    wx.openEmbeddedMiniProgram({
      appId: 'wx3e64b506e01256fd',
      //M1791331011611729920
      path: `/pages/pay/pay?storeId=${'M1747810615612809216'}&orderNo=${orderObj.sn}&orderAmount=${orderObj.pay_price}&public=1`,
      envVersion: 'release', //B的类型  trial体验版, ‘release’正式版    ‘develop’开发版
      success: function () {
        app.globalData.isSuccess = '1'
        app.globalData.pay_order_no =  orderObj.sn;

        console.log('跳转小程序成功');
        wx.request({
          url: rootDocment + '/api/Query',
          data: {
            order_sn: orderObj.sn,
            sign: sign
          },
          method: 'POST',
          header: {},
          success: function (res) {
            console.log('res支付跳转订单查询/api/Query', res)
          }
        })
        wx.request({
          url: rootDocment + '/api/query/cross',
          data: {
            order_sn: orderObj.sn,
            sign: sign
          },
          method: 'POST',
          header: {},
          success: function (res) {
            console.log('res支付跳转订单轮询查询/api/query/cross', res)
          }
        })
      },
      fail: function () {
        console.log('跳转小程序失败');
      }
    })
  }),
  createOrder() {
    return new Promise((resolve, reject) => {
      var paraArr = new Array();

      paraArr['user_id'] = this.data.userInfo.userID;
      console.log(this.data.userInfo, 'paraArr')
      var sign = app.signature(paraArr);
      wx.request({
        url: rootDocment + '/api/hehuoren/create',
        data: {
          user_id: paraArr['user_id'],
          sign: sign
        },
        method: 'POST',
        header: {},
        success: function (res) {
          console.log(res.data.data.sn, 'res')
          if (res.data.code == 1001) {
            resolve(res.data.data)
          } else {
            wx.showToast({
              title: res.data.msg,
            })
          }
        }
      })
    })
  },
  isPhone() {
    return new Promise((resolve, reject) => {
      var t = this
      var paraArr = new Array();
      paraArr['user_id'] = wx.getStorageSync('user_id3');
      var sign = app.signature(paraArr);
      wx.request({
        url: rootDocment + '/api/llgn/getVip',
        data: {
          user_id: paraArr['user_id'],
          sign: sign
        },
        method: 'POST',
        header: {},
        success: function (res) {
          if (res.data.code == 1003) {
            wx.showModal({
              title: '提示',
              content: res.data.msg || '绑定手机号才可购买',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/shouji/shouji',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }

              }
            })
            resolve(false)
          } else {
            resolve(true)
          }
        }
      })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})